package com.sdkx.app.dialog.markdialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.drawable.ColorDrawable;
import android.support.v7.widget.AppCompatCheckBox;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;

import com.sdkx.app.R;
import com.sdkx.app.ViewModel.MarkManager;
import com.sdkx.app.model.Mark;
import com.sdkx.app.util.ToastUtil;
import com.sdkx.app.util.Util;
import com.sdkx.app.view.DegreeInputView;

public class ModiMarkDialog extends Dialog implements View.OnClickListener {
    private Context context;

    private DegreeInputView lonedit, latedit;
    private EditText editRemarks, wpname;
    private Button btn_Confirm, btn_Cancel;
    private ImageButton markTypeButton;
    private String title;
    private AppCompatCheckBox mHideBox;
    private boolean isChecked = false;
    private int marktype = 0;
    private IMarkCallback iMarkCallback;
    private Mark mMark;

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.confirm:
                String markName = wpname.getText().toString();
                if (markName.equals("")) {
                    ToastUtil.show(getContext(), "名称不能为空", 1000);
                } else {
//                    if (MarkManager.getItem(markName) != null) {
//                        ToastUtil.show(context, "标记点名称重复！", 1000);
//                    } else
                    {
                        if (TextUtils.isEmpty(lonedit.getText().toString())) {
                            ToastUtil.show(context, "经度值为空", 1000);
                            return;
                        }
                        if (TextUtils.isEmpty(latedit.getText().toString())) {
                            ToastUtil.show(context, "纬度值为空", 1000);
                            return;
                        }
                        if (lonedit.checkillegal()) {
                            ToastUtil.show(context, "经度信息输入不合理", 1000);
                            return;
                        }
                        if (latedit.checkillegal()) {
                            ToastUtil.show(context, "纬度信息输入不合理", 1000);
                            return;
                        }
                        Mark mark = mMark;
                        if (mark == null)
                            mark = new Mark();
                        if (mark != null) {
                            mark.setLatitude(latedit.toDegree());
                            mark.setLongitude(lonedit.toDegree());
                            mark.setName(markName);
                            mark.setIsShow(!isChecked);
                            mark.setType(marktype);
                            mark.setDetails(editRemarks.getText().toString());
                        }
                        boolean flag = false;
                        if (mMark != null) {
                            int i = MarkManager.update(mMark);
                            flag = i >= 0;
                        } else {
                            flag = MarkManager.insert(mark);
                        }
                        if (flag) {
                            notifyMark(mark);
                            dismiss();
                        } else {
                            ToastUtil.show(context, "保存失败！", 5);
                        }
                    }

                }
                break;
            case R.id.cancel:
                //TODO implement
                dismiss();
                break;
            case R.id.mark_type:
                final MarkTypeDialog markTypeDialog = new MarkTypeDialog(context);
                markTypeDialog.setOnSelectListener(new MarkTypeDialog.IMarkTypeListener() {
                    @Override
                    public void onSelect(int position) {
                        Toast.makeText(getContext(), "marktype = " + position, Toast.LENGTH_SHORT).show();
                        MarkManager.MarkShape shape = MarkManager.getAllMarkShape().get(position);
                        markTypeButton.setImageResource(shape.drawableId);
                        //String info = wpname.getText().toString();
                        wpname.setText(shape.shapName);
                        marktype = position;
                        markTypeDialog.dismiss();
                    }
                });
                markTypeDialog.show();
                break;
        }
    }

    public ModiMarkDialog(Context activity, Mark mark) {
        super(activity);
        context = activity;
        mMark = mark;
        init();
    }


    public void init() {
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setBackgroundDrawable(new ColorDrawable(context.getResources().getColor(android.R.color.transparent)));

        Window dialogWindow = getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        DisplayMetrics d = getContext().getResources().getDisplayMetrics(); // 获取屏幕宽、高用
        //lp.x=-80;
        lp.width = (int) (d.widthPixels * 0.6); // 高度设置为屏幕的0.6
        lp.height = (int) (d.heightPixels * 0.4); // 高度设置为屏幕的0.6
        dialogWindow.setAttributes(lp);
        View contentview = getLayoutInflater().inflate(R.layout.mark_dlg_layout, null);
        setContentView(contentview);
        wpname = (EditText) contentview.findViewById(R.id.edit_wpname);
        latedit = (DegreeInputView) contentview.findViewById(R.id.edit_lat);
        lonedit = (DegreeInputView) contentview.findViewById(R.id.lon_edit);
        editRemarks = (EditText) contentview.findViewById(R.id.edit_remarks);
        btn_Confirm = (Button) contentview.findViewById(R.id.confirm);
        btn_Cancel = (Button) contentview.findViewById(R.id.cancel);
        mHideBox = (AppCompatCheckBox) contentview.findViewById(R.id.check_hide);
        btn_Confirm.setOnClickListener(this);
        btn_Cancel.setOnClickListener(this);
        markTypeButton = (ImageButton) contentview.findViewById(R.id.mark_type);
        markTypeButton.setOnClickListener(this);
        mHideBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean state) {
                isChecked = state;
            }
        });
        if (mMark != null) {
            wpname.setText(mMark.getName());
            latedit.setTextEx(Util.degreePatten(getContext(),mMark.getLatitude()));
            lonedit.setTextEx(Util.degreePatten(getContext(),mMark.getLongitude()));
            editRemarks.setText(mMark.getDetails());
            //isChecked = !mMark.getIsShow();
            markTypeButton.setImageResource(getTypeId(mMark.getType()));
            this.setOnShowListener(new OnShowListener() {
                @Override
                public void onShow(DialogInterface dialog) {
                    mHideBox.setChecked(!mMark.getIsShow());
                   // mHideBox.postInvalidate();
                }
            });

        }
    }

    private int getTypeId(int pos) {
        MarkManager.MarkShape shape = MarkManager.getAllMarkShape().get(pos);
        return shape.drawableId;
    }

    public void setTitleName(String name) {
        this.title = name;
    }

    public interface IMarkCallback {
        public void onSaveMark(Mark mark);
    }

    public void setiMarkCallback(IMarkCallback callback) {
        iMarkCallback = callback;
    }

    private void notifyMark(Mark mark) {
        if (iMarkCallback != null) {
            iMarkCallback.onSaveMark(mark);
        }
    }
}